<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/mi_project/css/index-top.css">
    <script src="/mi_project/layui/layui.js"></script>
    <link rel="stylesheet" href="/mi_project/layui/css/layui.css">
    <script src="/mi_project/js/jquery.js"></script>
    <script src="/mi_project/js/jquery.cookie.js"></script>
    <script src="/mi_project/js/common.js"></script>
    <link rel="stylesheet" href="/mi_project/css/orderNow.css">
</head>

<body style="width: 750px;height: 400px;padding-left: 10px;" class="orderNow">
    <div class="layui-form addressForm">
        <div>
            <h2>地址</h2>
            <div class="layui-collapse">
                <div class="layui-colla-item">
                    <h2 class="layui-colla-title">选择地址</h2>
                    <div class="layui-colla-content addressList">
                    </div>
                </div>

            </div>
        </div>
        <div>
            <h2>快递方式</h2>
            <input type="radio" name="expressWay" class="expressWay" value="1" title="快递">
            <input type="radio" name="expressWay" class="expressWay" value="" title="其他" disabled>
        </div>
        <div>
            <h2>支付方式</h2>
            <input type="radio" name="payWay" class="payWay" value="1" title="货到付款">
            <input type="radio" name="payWay" class="payWay" value="2" title="支付宝">
        </div>
        <div>
            <h2>发票类型</h2>
            <input type="radio" name="invoiceType" class="invoiceType" value="1" title="个人">
            <input type="radio" name="invoiceType" class="invoiceType" value="2" title="企业">
            <div>
                <input type="text" class="invoiceHeader" placeholder="发票抬头">
                <input type="text" class="invoiceContent" placeholder="发票内容">
            </div>
        </div>
        <div>
            <button type="button" class="layui-btn layui-btn-warm">确定支付</button>
        </div>
    </div>

</body>

</html>
<script>
    $.ajax({
        url: urls + '/address/lists',
        data: {
            userId: $.cookie('user').id
        },
        success: function (backData) {
            $('.addressList').html('');
            for (let key in backData.data) {
                $('.addressList').append(`<div class="iCenterBox" addressId="${backData.data[key].id}">
                <div>        
                <span>${backData.data[key].consignee}</span>
                        <span>${backData.data[key].mobile}</span>
                        <span>${backData.data[key].provinceName}</span>
                        <span>${backData.data[key].cityName}</span>
                        <span>${backData.data[key].areaName}</span>
                        <span
                            class="address">${backData.data[key].address}</span>
                            </div> 
                            <div>
                            <span class="addressChange">修改</span>
                            <span class="addressRemove">删除</span>
                            <span>设为默认</span>
                            <input type='checkbox' class='isDefault' isDefault="${backData.data[key].isDefault}">
                        </div>
                    </div>`);
            }
            let letCartId = 0;
            $('.isDefault').each(function (index, item) {
                if ($(item).attr('isDefault') == '1') {
                    letCartId = $(item).parent().parent().attr('addressId')
                    $(item).prop("checked", "true");
                } else {
                    $(item).prop("checked", null);
                }
            })
            $('.addressForm .layui-btn').click(function () {
                $.ajax({
                    url: urls + '/order/orderNow',
                    data: {
                        addressId: letCartId,
                        expressWay: $("input[name='expressWay']:checked").val(),
                        payWay: $("input[name='payWay']:checked").val(),
                        invoiceType: $("input[name='invoiceType']:checked").val(),
                        invoiceHeader: $('.invoiceHeader').val(),
                        invoiceContent: $('.invoiceContent').val(),
                        cartIds: JSON.parse(localStorage.getItem('checkboxId')),
                        userId: $.cookie('user').id
                    },
                    success: function (backData) {
                        if (backData.code == 0) {
                            layer.msg('下单成功');
                            localStorage.setItem('ok', JSON.stringify("ok"));
                            setTimeout(function () {
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭 
                            }, 2000)
                        } else {
                            layer.msg('下单失败');
                        }
                    }
                })

            })

        }
    })


    layui.form.render('select');

</script>